limsOutsourceApply.auditPage = {

    getGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: 'lims-outsource-apply-audit-list-grid',
            url: limsOutsourceApply.baseUrl + '/queries/tasks',
            columns: limsOutsourceApply.getAuditListColumns(),
            onCellClick: function (field, row) {
                field === 'applyCode' && _this.loadDetailPage(row.id)
            },
            requestData: {
                bpmn_statusCode: 'audit'
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.PASS',
                icon: 'pass',
                onClick: function () {
                    _this.pass()
                }
            }, {
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.REJECT',
                icon: 'reject',
                onClick: function () {
                    _this.reject()
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.COURIER.EDIT',
                icon: 'edit',
                onClick: function () {
                    _this.setCourier()
                }
            }]
        }
    },

    pass: function () {
        var grid = Gikam.getComp('lims-outsource-apply-audit-list-grid')
        Gikam.create('workflow').pass({
            data: grid.getSelections(),
            pageObject: limsOutsourceApply,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            grid.refresh()
        })
    },

    reject: function () {
        var grid = Gikam.getComp('lims-outsource-apply-audit-list-grid')
        Gikam.create('workflow').reject({
            data: grid.getSelections(),
            pageObject: limsOutsourceApply,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            grid.refresh()
        })
    },

    setCourier: function () {
        var grid = Gikam.getComp('lims-outsource-apply-audit-list-grid')
        var selections = grid.getSelections()
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
            return
        }
        Gikam.createLimsModal({
            title: 'GIKAM.BUTTON.COURIER.EDIT',
            height: 250,
            width: 400,
            onConfirm: function () {
                var form = Gikam.getComp('courier-edit-form')
                if (form.validate()) {
                    var data = form.getData()
                    if (Gikam.isEmpty(data)) {
                        return
                    }
                    let url = limsOutsourceApply.baseUrl + '/set-courier'
                    workspace.window.showMask()
                    Gikam.postText(url, Gikam.getJsonWrapper(data, [null, selections])).done(req => {
                        grid.refresh()
                        Gikam.getLastModal().close(data)
                    }).always(function () {
                        workspace.window.closeMask()
                    })
                }
            },
            m: {
                type: 'form',
                id: 'courier-edit-form',
                columns: 1,
                titleWidth: 80,
                disableNotEmptyValidate: true,
                fields: [{
                    field: 'courierNumber',
                    title: 'T_LIMS_OUTSOURCE_APPLY.COURIERNUMBER',
                    // validators: ['notEmpty'],
                }, {
                    field: 'courier',
                    title: 'T_LIMS_OUTSOURCE_APPLY.COURIER',
                    type: 'select',
                    firstBlank: false,
                    search: true,
                    category: 'sys',
                    param: {
                        codeCategoryId: 'courier'
                    },
                    // validators: ['notEmpty'],
                },],
            }
        })
    },

    loadDetailPage: function (id) {
        workspace.window.load(limsOutsourceApply.baseUrl + '/page/detail' + Gikam.param({
            id: id,
            step: 'audit'
        }))
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-outsource-apply-audit-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [this.getGridParam()]
            }
        })
    },

    init: function (param) {
        this.param = param
        this.create()
    }
}
